package com.jetlion.progressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    //进度条的初始值
    private int progress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ProgressBar progressBar = findViewById(R.id.progress);

        TextView textView = findViewById(R.id.textview);

        //通过handler去刷新界面
        Handler handler = new Handler();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                progress++;
                progressBar.setProgress(progress);
                textView.setText(progress + "%");

                if (progress < 100) {
                    //还没满100则再次调用该线程，this指向的是当前的Runnable
                    handler.postDelayed(this, 100);
                } else {
                    //满则移除线程
                    handler.removeCallbacks(this);
                }

            }
        }, 100);
    }
}